package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.Exam;
import com.ruoyi.system.domain.LearningContent;
import com.ruoyi.system.service.IExamService;
import com.ruoyi.system.service.ILearningContentService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AnswerRecordMapper;
import com.ruoyi.system.domain.AnswerRecord;
import com.ruoyi.system.service.IAnswerRecordService;
import com.ruoyi.common.core.text.Convert;

/**
 * 答题记录表Service业务层处理
 * 
 * @author admin
 * @date 2024-03-08
 */
@Service
public class AnswerRecordServiceImpl implements IAnswerRecordService 
{
    @Autowired
    private AnswerRecordMapper answerRecordMapper;
    @Autowired
    private IExamService examService;
    @Autowired
    private ILearningContentService learningContentService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询答题记录表
     * 
     * @param recordId 答题记录表主键
     * @return 答题记录表
     */
    @Override
    public AnswerRecord selectAnswerRecordByRecordId(Long recordId)
    {
        AnswerRecord answerRecord = answerRecordMapper.selectAnswerRecordByRecordId(recordId);
        Exam exam = examService.selectExamByExamId(answerRecord.getExamId());
        answerRecord.setQuestion(exam.getQuestion());
        answerRecord.setContentTitle(exam.getContentTitle());
        return answerRecord;
    }

    /**
     * 查询答题记录表列表
     * 
     * @param answerRecord 答题记录表
     * @return 答题记录表
     */
    @Override
    public List<AnswerRecord> selectAnswerRecordList(AnswerRecord answerRecord)
    {
        List<AnswerRecord> answerRecords = answerRecordMapper.selectAnswerRecordList(answerRecord);
        for (AnswerRecord record : answerRecords) {
            Exam exam = examService.selectExamByExamId(record.getExamId());
            record.setQuestion(exam.getQuestion());
            LearningContent learningContent = learningContentService.selectLearningContentByContentId(record.getContentId());
            SysUser sysUser = userService.selectUserById(record.getUserId());
            record.setUserName(sysUser.getUserName());
            record.setContentTitle(learningContent.getTitle());
        }
        return answerRecords;
    }

    /**
     * 新增答题记录表
     * 
     * @param answerRecordList 答题记录表
     * @return 结果
     */
    @Override
    public int insertAnswerRecord(List<AnswerRecord> answerRecordList, SysUser user)
    {
        for (AnswerRecord answerRecord : answerRecordList) {
            answerRecord.setUserId(user.getUserId());
            answerRecord.setSubmitTime(new Date());
            answerRecordMapper.insertAnswerRecord(answerRecord);
        }
        return 0;
    }

    /**
     * 修改答题记录表
     * 
     * @param answerRecord 答题记录表
     * @return 结果
     */
    @Override
    public int updateAnswerRecord(AnswerRecord answerRecord)
    {
        return answerRecordMapper.updateAnswerRecord(answerRecord);
    }

    /**
     * 批量删除答题记录表
     * 
     * @param recordIds 需要删除的答题记录表主键
     * @return 结果
     */
    @Override
    public int deleteAnswerRecordByRecordIds(String recordIds)
    {
        return answerRecordMapper.deleteAnswerRecordByRecordIds(Convert.toStrArray(recordIds));
    }

    /**
     * 删除答题记录表信息
     * 
     * @param recordId 答题记录表主键
     * @return 结果
     */
    @Override
    public int deleteAnswerRecordByRecordId(Long recordId)
    {
        return answerRecordMapper.deleteAnswerRecordByRecordId(recordId);
    }
}
